import React, {useEffect, useState} from "react"
import styles from "./EditEmployeeSettingFormDrawer.module.sass"
import FormDrawer, {FormDrawerProps} from "./FormDrawer";
import {UpdateEmployeeSettingRequest, updateMySetting} from "../apis/EmployeeApi";
import employeeAuthorizationStore from "../store/EmployeeAuthorizationStore";
import {Form, Switch, Typography} from "antd";
import {observer} from "mobx-react-lite";

export interface EditEmployeeSettingFormDrawerProps {
    open:boolean
    onClose:FormDrawerProps<UpdateEmployeeSettingRequest>["onClose"]
}

const EditEmployeeSettingFormDrawer: React.FC<EditEmployeeSettingFormDrawerProps> = (props) => {
    const [subscribeAllMarketOrderSubmittedWechatNotification,setSubscribeAllMarketOrderSubmittedWechatNotification] = useState(false)
    useEffect(() => {
        if (props.open){
            setSubscribeAllMarketOrderSubmittedWechatNotification(!!employeeAuthorizationStore.employee?.setting.subscribeAllMarketOrderSubmittedWechatNotification)
        }
    }, [props.open]);
    return <>
        <FormDrawer<UpdateEmployeeSettingRequest> title={"设置"}
                                                  onClose={props.onClose}
                                                  open={props.open}
                                                  onConfirm={async (values) =>{
                                                      await updateMySetting(values)
                                                      employeeAuthorizationStore.setEmployee({
                                                          ...employeeAuthorizationStore.employee!!,
                                                          setting:values,
                                                      })
                                                      props.onClose()
                                                  }}>
            {<Form.Item name={"subscribeAllMarketOrderSubmittedWechatNotification"}
                        label={"订阅所有卖场订单提交通知"}
                        layout={"horizontal"}>
                <Switch disabled={!employeeAuthorizationStore.hasAuthority("卖场_订单_查询所有")}
                        onChange={checked => setSubscribeAllMarketOrderSubmittedWechatNotification(checked)}
                        checked={subscribeAllMarketOrderSubmittedWechatNotification}/>
            </Form.Item>}
            <Form.Item label={"版本"} layout={"horizontal"}>
                <Typography.Text>{process.env.REACT_APP_APP_VERSION}</Typography.Text>
            </Form.Item>
        </FormDrawer>
    </>
}

export default observer(EditEmployeeSettingFormDrawer)